/* @(#)ProductTypeManager.java    V1.0
 *
 * Project: APEP 
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   ��ة��    {CREATE_TIME} First release
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2007 Auto-Parts E-Procurement Co., Ltd. 
 *        No.251-1 Wuning Road, Shanghai, 200063, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Auto-Parts E-Procurement Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 *   
 * 
 */
package system.base;

import java.util.ArrayList;

import com.hitrust.gs.dao.GSProductBidDao;
import com.hitrust.gs.dao.GSProductTypeDao;
import com.hitrust.gs.lib.GSBaseConstants;

import system.data.ProductTypeManagerPageData;
import framework.core.*;
/*
	Author    :��ة��
	CreateTime:{CREATE_TIME}
*/
public class ProductTypeManagerSrv extends IHandler
{

	public ProductTypeManagerSrv() 
	{
		super();
	}
	private void deleteProductTypeManager(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		ProductTypeManager obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = ProductTypeManager.fetch(con,String.valueOf(ids[i]));
			
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveProductTypeManager(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id");
		ProductTypeManager obj;
		ProductTypeManagerPageData data = (ProductTypeManagerPageData)PageData.fromForm(context,ProductTypeManagerPageData.class);
		if(id==null||id.equals(""))   //add
		{
			Conditions condition = new Conditions();
			condition.add(" upper_type != 0 and upper_type = '"+data.getUpperType()+"' order by  product_type desc");
	 		ObjectList list = ProductTypeManager.fetch().search(con,condition);
	 		int size = list.size();
	 		String num =String.valueOf (size+1);
			if(num.equals("99")){
				num=String.valueOf (size+2);
			}else{
				if(size>=99){
					ProductTypeManager p=(ProductTypeManager)list.get(0);
					num=String.valueOf(Integer.parseInt(p.getProductType())+1);
				}

			}
			Conditions conditions = new Conditions();
			conditions.add("(product_type ="+num+" or type_name = '"+data.getTypeName()+"' ) and upper_type = '"+data.getUpperType()+"'");
			obj = ProductTypeManager.fetch(con,conditions);
			if(obj!=null){
				context.setErrorMsg("类型或者类型名称已存在！");
				return;
			}else{
				obj = new ProductTypeManager();
				data.setCreateTime(new DateUtil().getFullString());
				data.setCreateUser(loginContext.getLoginUserName());
				data.setCreateUserId(loginContext.getLoginUserId());
				data.setProductType(num);
				data.setTypeLevel("2");
			}
    	}
    	else                     //update
    	{
    		obj = ProductTypeManager.fetch(con,String.valueOf(id));
    		if(obj==null){
				context.setErrorMsg("系统异常！");
				return;
    		}else{
    			Conditions conditions = new Conditions();
    			conditions.add("type_name = '"+data.getTypeName()+"' ");
    			ProductTypeManager ishas = ProductTypeManager.fetch(con,conditions);
    			if(ishas!=null&&(!ishas.getProductType().equals(obj.getProductType())||!ishas.getTableName().equals(obj.getTableName()))){
    				context.setErrorMsg("类型或者类型名称已存在！");
    				return;
    			}
	    		if(!data.getStatus().equals(obj.getStatus())&&data.getStatus().equals("0")){
	    			GSProductTypeDao tProductTypeDao = new GSProductTypeDao();
	    			if(ishas.getUpperType().equals(String.valueOf(GSBaseConstants.PRODUCT_TYPE_MANAGER_USE))){
		    			tProductTypeDao.updatePrudctTypeOrtherByStatus(ishas.getProductType(), con.getConnection());
	    			}else if(ishas.getUpperType().equals(String.valueOf(GSBaseConstants.PRODUCT_TYPE_MANAGER_BID))){
	    				GSProductBidDao productBidDao = new GSProductBidDao();
	    				tProductTypeDao.updatePrudctTypeBidByStatus(ishas.getProductType(),con.getConnection());
	    				productBidDao.updatePrudctBidOtherByStatus(ishas.getProductType(), con.getConnection());
	    			}
	    			
	    		}
	    		obj.setTypeName(data.getTypeName());
	    		obj.setRemark(data.getRemark());
    		}
    	}
		data.setLastUpdateTime(new DateUtil().getFullString());
		data.setLastUpdateUser(loginContext.getLoginUserName());
		data.setLastUpdateUserId(loginContext.getLoginUserId());
		context.setValue("ProductTypeManagerPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listProductTypeManager(DBConnection con,SrvContext context,String path) throws Exception
	{
		ProductTypeManagerSearchEvent event = (ProductTypeManagerSearchEvent)SearchEvent.fromForm(context,ProductTypeManagerSearchEvent.class);
		context.setSessionValue("ProductTypeManagerSearchEvent",event);
		context.setNextUrl(path+"ProductTypeManagerList.jsp");
	}
	private void backProductTypeManager(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"ProductTypeManagerList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listProductTypeManager"))
		{
			listProductTypeManager(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewProductTypeManager"))
		{
			String id = context.getString("Id");
			ProductTypeManager obj = ProductTypeManager.fetch(con,id);
			context.setValue("ProductTypeManager",obj);
			context.setNextUrl(path+"ProductTypeManagerView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteProductTypeManager"))
		{
			context.setErrorUrl(path+"ProductTypeManagerList.jsp");
			deleteProductTypeManager(con,context);
			backProductTypeManager(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddProductTypeManager"))
		{
			ProductTypeManager obj = ProductTypeManager.fetch();
			ProductTypeManagerPageData data = (ProductTypeManagerPageData)PageData.fromTable(obj,ProductTypeManagerPageData.class);
			context.setValue("ProductTypeManagerPageData",data);
			context.setNextUrl(path+"ProductTypeManagerEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateProductTypeManager"))
		{
			String id = context.getString("Id");
			ProductTypeManager obj = ProductTypeManager.fetch(con,id);
			ProductTypeManagerPageData data = (ProductTypeManagerPageData)PageData.fromTable(obj,ProductTypeManagerPageData.class);
			context.setValue("ProductTypeManagerPageData",data);
			context.setNextUrl(path+"ProductTypeManagerEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyProductTypeManager"))
		{
			String id = context.getString("Id");
			ProductTypeManager obj = ProductTypeManager.copy(ProductTypeManager.fetch(con,id));
			ProductTypeManagerPageData data = (ProductTypeManagerPageData)PageData.fromTable(obj,ProductTypeManagerPageData.class);
			context.setValue("ProductTypeManagerPageData",data);
			context.setNextUrl(path+"ProductTypeManagerEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveProductTypeManager"))
		{
			context.setErrorUrl(path+"ProductTypeManagerEdit.jsp");
			saveProductTypeManager(con,context,loginContext);
			backProductTypeManager(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewProductTypeManager"))
		{
			context.setErrorUrl(path+"ProductTypeManagerEdit.jsp");
			saveProductTypeManager(con,context,loginContext);
			ProductTypeManager obj = ProductTypeManager.fetch();
			ProductTypeManagerPageData data = (ProductTypeManagerPageData)PageData.fromTable(obj,ProductTypeManagerPageData.class);
			context.setValue("ProductTypeManagerPageData",data);
			context.setNextUrl(path+"ProductTypeManagerEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListProductTypeManager"))
		{
			backProductTypeManager(con,context,path);
			return;
		}

		Logger.throwException("ProductTypeManagerSrv","unknown action_code="+action_code);
	}
}
